package org.feng.clog.config;

import org.feng.clog.utils.ResultUtil;
import org.feng.clog.vo.ResultVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.lang.invoke.MethodHandles;

import static org.springframework.http.HttpStatus.OK;

/**
 * 全局异常处理
 *
 * @author feng
 */
@RestControllerAdvice
public class ExceptionInterceptor {

    private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

    /**
     * 工程自定义异常
     *
     * @param exception 未处理异常
     * @return ResultVo
     */
    @ExceptionHandler(RuntimeException.class)
    @ResponseStatus(OK)
    public ResultVo<String> handleCommonException(RuntimeException exception) {
        LOGGER.warn("RuntimeException occurred ! ", exception);
        return ResultUtil.error(500, exception.getMessage());
    }
}
